package com.gopay.common.cps.dao.acct.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.acct.PayNuccRulesDAO;
import com.gopay.common.domain.bank.route.po.pay.PayNuccRules;
/**
 * 网联通道出款账户自动切换规则DAO实现
 *
 * @author lynn
 * @data
 */
@Repository("payNuccRulesDAO")
public class PayNuccRulesDAOImpl extends BaseDAOImpl<PayNuccRules, String> implements PayNuccRulesDAO {


	@Override
	public List<PayNuccRules> findPayNuccRulesByChannId(String channelId) {
		StringBuffer hql = new StringBuffer();
		hql.append("from PayNuccRules where channelId = :channelId ");
		hql.append("and isValid = :isValid");
		hql.append("and delFlag = :delFlag");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("channelId", channelId);
		param.put("isValid", "1");
		param.put("delFlag", "1");
		List<PayNuccRules> payNuccRules = this.findByHql(hql.toString(), param);
		return payNuccRules;
	}

	@Override
	public List<PayNuccRules> findPayNuccRulesByChannId(long channelId, String whFlag, String curDate, BigDecimal bankAcctAmt) {
		StringBuffer hql = new StringBuffer();
		hql.append("from PayNuccRules where channelId = :channelId ");
		hql.append("and (whFlag = :whFlag or whFlag = :whFlag0) ");
		hql.append("and minAmt < :bankAcctAmt and maxAmt >= :bankAcctAmt ");
		hql.append("and startTime <= :curDate and endTime >= :curDate ");
		hql.append("and isValid = :isValid ");
		hql.append("and delFlag = :delFlag");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("channelId", channelId);
		param.put("whFlag", whFlag);
		param.put("whFlag0", "0");
		param.put("bankAcctAmt", bankAcctAmt);
		param.put("curDate", curDate);
		param.put("isValid", "1");
		param.put("delFlag", "1");
		List<PayNuccRules> payNuccRules = this.findByHql(hql.toString(), param);
		return payNuccRules;
	}

	@Override
	public PayNuccRules findPayNuccRulesByChannIdAndRollFlag(long channelId, String rollBackFlag) {
		StringBuffer hql = new StringBuffer();
		hql.append("from PayNuccRules where channelId = :channelId ");
		hql.append("and rollbackFlag = :rollbackFlag ");
		hql.append("and isValid = :isValid ");
		hql.append("and delFlag = :delFlag");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("channelId", channelId);
		param.put("rollbackFlag", rollBackFlag);
		param.put("isValid", "1");
		param.put("delFlag", "1");
		List<PayNuccRules> payNuccRules = this.findByHql(hql.toString(), param);
		if(null != payNuccRules && payNuccRules.size() != 0){
			return payNuccRules.get(0);
		}else{
			return null;
		}
	}

}
